package demoMod.invincibleOne.cards.invincible;

import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.rewards.RewardItem;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.interfaces.AddRewardToCombatScreenHook;
import demoMod.invincibleOne.interfaces.IOnAddToMasterDeck;
import demoMod.invincibleOne.relics.TaskGoal;

public class Armaments extends AbstractInvincibleCard implements AddRewardToCombatScreenHook, IOnAddToMasterDeck {
    public static final String ID = InvincibleOneMod.makeID("Armaments");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/Armaments.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.COMMON;
    private static final CardTarget TARGET = CardTarget.NONE;

    private static final int COST = -2;

    public Armaments() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.baseMagicNumber = this.magicNumber = 2;
        this.tags.add(CardTagsEnum.ALERT);
    }

    @Override
    public void onAddToMasterDeck() {
        if (AbstractDungeon.player.hasRelic(TaskGoal.ID)) {
            AbstractRelic relic = AbstractDungeon.player.getRelic(TaskGoal.ID);
            if (relic.counter < 0) {
                relic.counter = 0;
            }
            relic.counter += this.magicNumber;
        }
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            upgradeMagicNumber(1);
            if (AbstractDungeon.player != null && AbstractDungeon.player.masterDeck.contains(this)) {
                if (AbstractDungeon.player.hasRelic(TaskGoal.ID)) {
                    AbstractRelic relic = AbstractDungeon.player.getRelic(TaskGoal.ID);
                    relic.counter++;
                }
            }
        };
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {

    }

    @Override
    public void onAddRewardToCombatRewardScreen(RewardItem item) {
        if (item.type == RewardItem.RewardType.CARD) {
            if (AbstractDungeon.player.hasRelic(TaskGoal.ID)) {
                AbstractRelic relic = AbstractDungeon.player.getRelic(TaskGoal.ID);
                if (relic.counter > 0) {
                    if (item.cards.stream().noneMatch(AbstractCard::canUpgrade)) {
                        return;
                    }
                    relic.counter--;
                    item.cards.forEach(c -> {
                        if (c.canUpgrade()) {
                            c.upgrade();
                        }
                    });
                }
            }
        }
    }
}
